package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.mapper.CIDCompleteMapper;
import com.gsta.cdn.cdr.mapper.CIDConcurrentMapper;
import com.gsta.cdn.cdr.service.CIDCompleteService;
import com.gsta.cdn.cdr.service.CIDConcurrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/CIDComplete")
public class CIDCompleteController {
    @Autowired
    private CIDCompleteMapper cidCompleteMapper;

    @Resource
    private CIDCompleteService cidCompleteService;

    //播放统计
    @RequestMapping(path = "/CIDCompleteAll", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteAll(@RequestParam(required = true) String date,
                                            @RequestParam(required = false,defaultValue = "1")int page,
                                            @RequestParam(required = false,defaultValue = "200")int pageSize,
                                            HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteAll(date,page,pageSize);
        int total = cidCompleteMapper.CIDCompleteAllCount(date);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //播放统计地区列表
    @RequestMapping(path = "/CIDCompleteArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteArea(@RequestParam(required = true) String date,
                                       HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteArea(date);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区播放统计
    @RequestMapping(path = "/CIDCompleteByArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteByArea(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String area,
                                       @RequestParam(required = false,defaultValue = "1")int page,
                                       @RequestParam(required = false,defaultValue = "200")int pageSize,
                                       HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteByArea(date,area,page,pageSize);
        int total = cidCompleteMapper.CIDCompleteByAreaCount(date,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //播放统计地区的PopName列表
    @RequestMapping(path = "/CIDCompleteAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteAreaPopName(@RequestParam(required = true) String date,
                                               @RequestParam(required = true) String area,
                                        HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteAreaPopName(date,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区PopName播放统计
    @RequestMapping(path = "/CIDCompleteByAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteByAreaPopName(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String area,
                                                 @RequestParam(required = true) String popName,
                                          @RequestParam(required = false,defaultValue = "1")int page,
                                          @RequestParam(required = false,defaultValue = "200")int pageSize,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteByAreaPopName(date,area,popName,page,pageSize);
        int total = cidCompleteMapper.CIDCompleteByAreaPopNameCount(date,area,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //PopName播放统计
    @RequestMapping(path = "/CIDCompleteByPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity CIDCompleteByPopName(@RequestParam(required = true) String date,
                                                 @RequestParam(required = true) String popName,
                                                 @RequestParam(required = false,defaultValue = "1")int page,
                                                 @RequestParam(required = false,defaultValue = "200")int pageSize,
                                                 HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = cidCompleteService.CIDCompleteByPopName(date,popName,page,pageSize);
        int total = cidCompleteMapper.CIDCompleteByPopNameCount(date,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }
}
